package com.bai.easyui.servlet;

import com.bai.easyui.Dao.UserDao;
import com.bai.easyui.DaoImp.UserDaoImp;
import com.bai.easyui.bean.City;
import com.bai.easyui.bean.CriteriaUser;
import com.bai.easyui.bean.User;
import net.sf.json.JSONArray;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Admin on 2/10 0010.
 */
@WebServlet("/userServlet")
public class UserServlet extends HttpServlet {
    private UserDao ud=new UserDaoImp();
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        String method = request.getParameter("method");
        System.out.println(method);
        if("getCity".equals(method)){
            getCity(request,response);
        }else if("save".equals(method)){
            save(request,response);
        }else if("getList".equals(method)){
            getList(request,response);
        }else if("update".equals(method)){
            update(request,response);
        }else if("delete".equals(method)){
            delete(request,response);
        }
    }

    private void delete(HttpServletRequest request, HttpServletResponse response) {
        try {
            String[] ids = request.getParameter("ids").split(",");
            System.out.println(ids.length);
            for (int i = 0; i < ids.length; i++) {
               ud.delete(Integer.parseInt(ids[i]));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void update(HttpServletRequest request, HttpServletResponse response){
        try {
            Integer id = Integer.parseInt(request.getParameter("id"));
            System.out.println("............"+id);
           // User user = ud.findById(id);
            String name = request.getParameter("name");
            String password = request.getParameter("password");
            String sex = request.getParameter("sex");
            Integer age = Integer.parseInt(request.getParameter("age"));
            String birthday = request.getParameter("birthday");
            Integer city = Integer.parseInt(request.getParameter("city"));
            String salary = request.getParameter("salary");
            String startTime = request.getParameter("startTime");
            String endTime = request.getParameter("endTime");
            String description = request.getParameter("description");

//            user.setName(name);
//            user.setPassword(password);
//            user.setSex(sex);
//            user.setAge(age);
//            user.setBirthday(birthday);
//            user.setCity(city);
//            user.setSalary(salary);
//            user.setStartTime(startTime);
//            user.setEndTime(endTime);
//            user.setDescription(description);
            User user=new User(id,name,password,sex,age,birthday,city,salary,startTime,endTime,description);
            ud.edit(user);

            response.setContentType("text/html;charset=utf-8");
            String str = "{\"status\":\"ok\" , \"message\":\"操作成功!\"}";
            response.getWriter().write(str);
        } catch (IOException e) {
            response.setContentType("text/html;charset=utf-8");
            String str = "{\"status\":\"fail\" , \"message\":\"操作失败!\"}";
            try {
                response.getWriter().write(str);
            } catch (IOException e1) {
                e1.printStackTrace();
            }
            e.printStackTrace();
        }
    }

    private void getList(HttpServletRequest request, HttpServletResponse response) {
        try{
//            List<User> users=ud.getAll();
            Integer page=Integer.parseInt(request.getParameter("page"));//当前页的页码
            Integer pageSize=Integer.parseInt(request.getParameter("rows"));//每页的记录数
            String name=request.getParameter("name");
            String startTime=request.getParameter("startTime");
            String endTime=request.getParameter("endTime");
            String order  = request.getParameter("order");		//排序的方式
            String sort  = request.getParameter("sort");			//排序的字段
            CriteriaUser cu=new CriteriaUser(name,startTime,endTime);
           // List<User> uu=ud.getForListWithCriteriaUser(cu);
            List<User> users=ud.getPagination(page,pageSize,cu);
            long totla=ud.getTotal(cu);
            response.setContentType("text/html;charset=utf-8");
            //{"total":10 , "rows":[{},{}]}
            String json="{\"total\":"+totla+",\"rows\":"+JSONArray.fromObject(users).toString()+"}";
            response.getWriter().write(json);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    private void save(HttpServletRequest request, HttpServletResponse response) {
        try {
          //  User user= (User) FormUtils.parseForm(request,User.class,"utf-8");
            String name = request.getParameter("name");
            String password = request.getParameter("password");
            String sex = request.getParameter("sex");
            Integer age = Integer.parseInt(request.getParameter("age"));
            String birthday = request.getParameter("birthday");
            Integer city = Integer.parseInt(request.getParameter("city"));
            String salary = request.getParameter("salary");
            String startTime = request.getParameter("startTime");
            String endTime = request.getParameter("endTime");
            String description = request.getParameter("description");
            User user=new User(null,name,password,sex,age,birthday,city,salary,startTime,endTime,description);
            ud.save(user);
            response.setContentType("text/html;charset=utf-8");
            //{"status":"ok" , "message":"操作成功"}
            String str = "{\"status\":\"ok\" , \"message\":\"操作成功!\"}";
            response.getWriter().write(str);
        } catch (IOException e) {
            response.setContentType("text/html;charset=utf-8");
            //{"status":"fail" , "message":"操作失败!"}
            String str = "{\"status\":\"fail\" , \"message\":\"操作失败!\"}";
            try {
                response.getWriter().write(str);
            } catch (IOException e1) {
                e1.printStackTrace();
            }
            e.printStackTrace();
        }
//        catch (Exception e) {
//            e.printStackTrace();
//        } catch (InvocationTargetException e) {
//            e.printStackTrace();
//        } catch (NoSuchMethodException e) {
//            e.printStackTrace();
//        } catch (IllegalAccessException e) {
//            e.printStackTrace();
//        } catch (NoSuchFieldException e) {
//            e.printStackTrace();
//        }


    }

    private void getCity(HttpServletRequest request, HttpServletResponse response) {
        try {
            List<City> clist = new ArrayList<>();
            clist.add(new City(1,"北京"));
            clist.add(new City(2,"上海"));
            clist.add(new City(3,"深圳"));
            clist.add(new City(4,"长春"));
            response.setContentType("text/html;charset=utf-8");
            String str = JSONArray.fromObject(clist).toString();
            response.getWriter().write(str);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
